package app.backend.service.impl;

import app.backend.common.CommonException;
import app.backend.entity.AppClientUser;
import app.backend.mapper.AppClientUserMapper;
import app.backend.service.AppClientUserService;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.stereotype.Service;


import java.time.LocalDateTime;

/**
 * <p>ClientUserMapper
 * 微信小程序端用户表 服务实现类
 * </p>
 *
 * @author 作者
 * @since 2022-08-16
 */
@Service
@Slf4j
public class AppClientUserServiceImpl extends ServiceImpl<AppClientUserMapper, AppClientUser> implements AppClientUserService {
    @Resource
    private WxMaService wxMaService;

    @Override
    public AppClientUser login(String code,String jsCode) {
        try {
            String openid = wxMaService.jsCode2SessionInfo(code).getOpenid();
            WxMaPhoneNumberInfo wxMaPhoneNumberInfo = wxMaService.getUserService().getPhoneNoInfo(jsCode);
            String phone = wxMaPhoneNumberInfo.getPhoneNumber();
            if(openid == null){
                throw new CommonException("获取微信用户失败");
            }
            if(phone == null){
                throw new CommonException("获取手机号失败");
            }
            AppClientUser clientUser = baseMapper.selectOne(Wrappers.<AppClientUser>lambdaQuery()
                   .eq(AppClientUser::getOpenid, openid));
            if(clientUser!=null){
                clientUser.setLoginTime(LocalDateTime.now());
                baseMapper.updateById(clientUser);
                return clientUser;
            } else {
                AppClientUser user = new AppClientUser();
                user.setOpenid(openid);
                user.setPhone(phone);
                user.setNickname("User_"+openid.substring(openid.length()-10));
                user.setLoginTime(LocalDateTime.now());
                baseMapper.insert(user);
                return baseMapper.selectOne(Wrappers.<AppClientUser>lambdaQuery()
                        .eq(AppClientUser::getOpenid, openid));
            }

        } catch (WxErrorException e) {
            log.error("获取微信用户信息失败", e);
        }
        return null;
    }

    @Override
    public AppClientUser getUser(String code) {
        String openid = null;
        try {
            openid = wxMaService.jsCode2SessionInfo(code).getOpenid();
        } catch (WxErrorException e) {
            log.error("获取微信用户信息失败", e);
        }
        return baseMapper.selectOne(Wrappers.<AppClientUser>lambdaQuery()
                .eq(AppClientUser::getOpenid, openid));
    }


    @Override
    public AppClientUser updateClientUser(AppClientUser registerInfo) {
        baseMapper.updateById(registerInfo);
        return baseMapper.selectById(registerInfo.getId());
    }

    @Override
    public AppClientUser getUserInfo(String userId) {
        return baseMapper.selectById(userId);
    }
}
